
import { Pusher } from "./pusher";
import { Player } from "./player";

window.localStorage.setItem('debug', '*');

let pusher;
let player;
let roomId;

window.onload = function() {
    document.getElementById('publish-button').onclick = publish;
    document.getElementById('play-button').onclick = play; 

    roomId = document.getElementById('roomId').innerText; 
}

async function publish() {
    let videoElement = document.getElementById('video_container_publish');

    // 这里的pushId是随便给的，业务层应该根据实际情况给一个
    pusher = new Pusher(roomId, roomId+":pushId");

    await pusher.init(videoElement);
}

async function play() {
    let videoElement = document.getElementById('video_container_play');

    let selfPeerId = roomId + "viewer" + Math.round(Math.random() * 100000);
    player = new Player(roomId, selfPeerId, roomId+":pushId");

    videoElement.onloadedmetadata = function () {
        console.log('onloadedmetadata ' + Date.now());
    }

    await player.init(videoElement);
}